package org.softmed.rest.admin.mediaType;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import org.restlet.data.MediaType;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.rest.admin.module.ValidationException;
import org.softmed.rest.admin.interfaces.Recompiler;
import org.softmed.rest.admin.interfaces.Validator;

public class MediaTypeChooser extends JPanel implements ActionListener {

	protected static List<String> mediaTypes = new ArrayList<String>();

	Recompiler recompiler;
	String mediaTypeDescription;
	Validator validator;

	static {
		Class<MediaType> type = MediaType.class;

		Field[] fields = type.getDeclaredFields();

		String name = null;
		for (Field field : fields) {
			name = field.getName();
			int modifiers = field.getModifiers();

			if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) {
				mediaTypes.add(name);
			}

		}

		// for (String string : mediaTypes) {
		// System.out.println(string);
		// }

	}

	JComboBox mediaTypeList;

	private boolean fireEvent = true;

	public MediaTypeChooser() {
//		setBackground(Color.YELLOW);
		// setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		setLayout(new GridLayout(0, 1));

		setPreferredSize(new Dimension(240, 25));
		setMaximumSize(new Dimension(240, 25));
		setMinimumSize(new Dimension(240, 25));

		mediaTypeList = new JComboBox(mediaTypes.toArray());
		mediaTypeList.setSelectedItem(null);

		mediaTypeList.setPreferredSize(new Dimension(240, 25));
		mediaTypeList.setMaximumSize(new Dimension(240, 25));
		mediaTypeList.setMinimumSize(new Dimension(240, 25));

		mediaTypeList.addActionListener(this);

		mediaTypeList.setAlignmentX(Component.LEFT_ALIGNMENT);
		mediaTypeList.setAlignmentY(Component.TOP_ALIGNMENT);

		add(mediaTypeList);

	}

	public String getMediaTypeDescription() {
		return mediaTypeDescription;
	}

	public void setMediaTypeDescription(String mediaTypeDescription) {
		fireEvent = false;
		this.mediaTypeDescription = mediaTypeDescription;
		mediaTypeList.setSelectedItem(mediaTypeDescription);
		fireEvent = true;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (!fireEvent)
			return;

		mediaTypeDescription = (String) mediaTypeList.getSelectedItem();
		// System.out.println(mediaTypeDescription);
		if (validator.validate(this))
			recompiler.recompile();
				else
				{
					//TODO show error
					System.out.println("MEDIA TYPE ALREADY TAKEN");
				}
			
	}

	public void validateData() throws ValidationException {
		if (mediaTypeList.getSelectedIndex() < 0)
			throw new ValidationException("Media Type must be selected !!! ");

	}

	public Validator getValidator() {
		return validator;
	}

	public void setValidator(Validator validator) {
		this.validator = validator;
	}

	public Recompiler getRecompiler() {
		return recompiler;
	}

	public void setRecompiler(Recompiler recompiler) {
		this.recompiler = recompiler;
	}

}
